package com.yxw.yx_hotel.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxw.yx_hotel.dto.AddRoomFeeDto;
import com.yxw.yx_hotel.dto.QueryRoomFeeDto;
import com.yxw.yx_hotel.dto.UpdateRoomFeeDto;
import com.yxw.yx_hotel.entity.YxFileEntity;
import com.yxw.yx_hotel.entity.YxRoomFeeEntity;
import com.yxw.yx_hotel.mapper.RoomFeeMapper;
import com.yxw.yx_hotel.service.FileService;
import com.yxw.yx_hotel.service.RoomFeeService;
import com.yxw.yx_hotel.utils.PageUtils;
import com.yxw.yx_hotel.utils.UserUtil;
import com.yxw.yx_hotel.vo.RoomFeeVo;
import com.yxw.yx_hotel.vo.UserTokenVo;
import com.yxw.yxnet_cd_center.common.dto.PageVO;
import com.yxw.yxnet_cd_center.common.exception.BaseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *  宿舍费用管理
 * @author liuxin
 * @date 2024.01.17
 */
@Service
public class RoomFeeServiceImpl extends ServiceImpl<RoomFeeMapper, YxRoomFeeEntity> implements RoomFeeService {

    @Autowired
    private UserUtil  userUtil;
    @Autowired
    private FileService  fileService;
    /**
     * 新增宿舍费用
     * @param dto
     */
    @Override
    @Transactional(rollbackFor=Exception.class)
    public void addRoomFee(AddRoomFeeDto dto) {
        UserTokenVo userVo = userUtil.getLoginUserVo();
        YxRoomFeeEntity entity = new YxRoomFeeEntity();
        BeanUtil.copyProperties(dto,entity);
        entity.setCreateTime(new Date());
        entity.setCreateName(userVo.getNickName());
        entity.setTenantId(userVo.getTenantId());
        entity.setCreateId(userVo.getUserId());
        baseMapper.insert(entity);
    }

    /**
     * 查询宿舍资产列表
     * @param dto
     * @return
     */
    @Override
    public PageVO<RoomFeeVo> pageRoomFeeList(QueryRoomFeeDto dto) {
        List<RoomFeeVo> voList=new ArrayList<>();
        UserTokenVo userVo = userUtil.getLoginUserVo();
        Page<YxRoomFeeEntity> page = new Page<>(dto.getCurrPage(),dto.getPageSize());
        QueryWrapper<YxRoomFeeEntity>  queryWrapper=new QueryWrapper();
        queryWrapper.eq("room_id",dto.getRoomId());
        queryWrapper.eq("tenant_id",userVo.getTenantId());
        queryWrapper.orderByDesc("create_time");
        Page<YxRoomFeeEntity> entityPage = baseMapper.selectPage(page, queryWrapper);
        List<YxRoomFeeEntity> records = entityPage.getRecords();
        if(null!=records && !records.isEmpty()){
            voList=BeanUtil.copyToList(records,RoomFeeVo.class);
        }
        return PageUtils.get(page, voList);
    }

    /**
     * 查询费用详情
     * @param feeId
     * @return
     */
    @Override
    public  RoomFeeVo getRoomFeeById(Long feeId) {
        RoomFeeVo vo = new RoomFeeVo();
        YxRoomFeeEntity entity = baseMapper.selectById(feeId);
        if(null!=entity){
            BeanUtil.copyProperties(entity,vo);
        }
        List<YxFileEntity> fileList = fileService.getFilesByBussId(feeId);
        vo.setPictureList(fileList);
        return vo;
    }

    /**
     * 更新宿舍报修信息
     * @param dto
     */
    @Override
    @Transactional(rollbackFor=Exception.class)
    public void updateRoomFee(UpdateRoomFeeDto dto){
        UserTokenVo userVo = userUtil.getLoginUserVo();
        YxRoomFeeEntity entity = baseMapper.selectById(dto.getFeeId());
        if(null==entity){
            throw new BaseException("未查询到费用信息进行修改");
        }
        BeanUtil.copyProperties(dto,entity);
        entity.setUpdateId(userVo.getUserId());
        entity.setUpdateName(userVo.getNickName());
        entity.setUpdateTime(new Date());
        baseMapper.updateById(entity);
    }
}
